package com.csun.cmny.provider.controller;

import cn.jiguang.common.utils.Preconditions;
import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcExpenseItem;
import com.csun.cmny.provider.model.domain.OmcOldmanExpenseItem;
import com.csun.cmny.provider.model.dto.OldmanExpenseItemDto;
import com.csun.cmny.provider.model.dto.SelectExpenseDto;
import com.csun.cmny.provider.model.dto.check_in.CheckInDaysDto;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.model.vo.check_in.CheckInDaysVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcOldmanExpenseItemService;
import com.csun.cmny.provider.service.OmcOldmanPaidExpenseItemService;
import com.csun.cmny.provider.service.OmcReturnFeeConfigService;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.validator.SelectExpenseAddValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping(value = "/selectExpense", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSelectExpenseController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcOldmanExpenseItemService omcOldmanExpenseItemService;

    @Resource
    private OmcOldmanPaidExpenseItemService omcOldmanPaidExpenseItemService;

    @Resource
    private OmcReturnFeeConfigService omcReturnFeeConfigService;


    /**
     * 缴费项目-查询
     * @param: [modelCode]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryExpenseItem/{modelCode}")
    public Wrapper queryInExpenseItem(@PathVariable Integer modelCode){

        // 校验Id是否为空
        Preconditions.checkArgument(modelCode != null, cmnyMessageSource
                .getMessage("ExpenseType.modelCode.null", null));
        List<ExpenseTypeVo> expenseTypeVoList = omcOldmanExpenseItemService.queryExpenseItem(modelCode,getLoginAuthDto());
        //return WrapMapper.ok(expenseTypeVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),expenseTypeVoList);
    }

    /**
     * 缴费项目-保存选择项目
     * @param: [oldmanExpenseItemDtoList]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/bindExpenseItem")
    public Wrapper bindExpenseItem(@RequestBody List<OldmanExpenseItemDto> oldmanExpenseItemDtoList){

        List<OmcOldmanExpenseItem> omcOldmanExpenseItemList = new LinkedList<>();

        for(OldmanExpenseItemDto oldmanExpenseItemDto : oldmanExpenseItemDtoList){
            OmcOldmanExpenseItem omcOldmanExpenseItem = new OmcOldmanExpenseItem();
            BeanUtils.copyProperties(oldmanExpenseItemDto,omcOldmanExpenseItem);
            omcOldmanExpenseItemList.add(omcOldmanExpenseItem);
        }

        int ret = omcOldmanExpenseItemService.bindExpenseItem(omcOldmanExpenseItemList,getLoginAuthDto());

        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 套餐匹配
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/matchSetMeal/{oldmanId}")
    public Wrapper matchSetMeal(@PathVariable Long oldmanId){
      // 校验Id是否为空
        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        List<SetMealVo> result = omcOldmanExpenseItemService.matchSetMeal(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }


    /**
     * 查询老人选择的项目
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryOldmanExpenseItem/{oldmanId}")
    public Wrapper queryOldmanExpenseItem(@PathVariable Long oldmanId){

        // 校验Id是否为空
        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        List<ExpenseItemVo> expenseItemVoList = omcOldmanExpenseItemService.queryOldmanExpenseItem(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(expenseItemVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),expenseItemVoList);
    }

    /**
     * 生成入院缴费单
     * @param: [selectExpenseDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/generateOldmanInBill")
    public Wrapper generateOldmaInBill(@Validated(SelectExpenseAddValidator.class)@RequestBody SelectExpenseDto selectExpenseDto, BindingResult br){

        int ret = omcOldmanExpenseItemService.generateOldmaInBill(selectExpenseDto,getLoginAuthDto());

        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }


    /**
     * 匹配床位费
     * @param: [bedId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/matchBedFeeByBedId/{bedId}")
    public Wrapper mathcBedFeeByBedId(@PathVariable Long bedId){
     // 校验Id是否为空
        Preconditions.checkArgument(bedId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        OmcExpenseItem vo = omcOldmanExpenseItemService.mathcBedFeeByBedId(bedId,getLoginAuthDto());
        //return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),vo);
    }

    /**
     * 查询咨询记录中的床位id
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getAdvisoryBedId/{oldmanId}")
    public Wrapper getAdvisoryBedId(@PathVariable Long oldmanId){
        // 校验Id是否为空
       Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        Long bedId = omcOldmanExpenseItemService.getAdvisoryBedId(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(bedId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),bedId);
    }

    /**
     * 暂存老人选择的床位id
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/setSelectBedId")
    public Wrapper setSelectBedId(@RequestParam Long oldmanId,@RequestParam Long bedId){
        // 校验Id是否为空
        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("bed.id.null", null));
        int ret = omcOldmanExpenseItemService.setSelectBedId(oldmanId,bedId,getLoginAuthDto());
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 删除某个流程选择的缴费项目
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteSelectItemByModelCode")
    public Wrapper deleteSelectItemByModelCode(@RequestParam Long oldmanId,@RequestParam Integer modelCode){
        // 校验Id是否为空
        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        Preconditions.checkArgument(modelCode != null, cmnyMessageSource
                .getMessage("ExpenseType.modelCode.null", null));
        int ret = omcOldmanExpenseItemService.deleteSelectItemByModelCode(oldmanId,modelCode,getLoginAuthDto());
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 护理费匹配
     * @param: [careType]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/matchCareFeeByCareType/{levelCareId}")
    public Wrapper mathcCareFeeByCareType(@PathVariable Long levelCareId){
       // 校验Id是否为空
        Preconditions.checkArgument(levelCareId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        OmcExpenseItem vo = omcOldmanExpenseItemService.mathcCareFeeByCareType(levelCareId,getLoginAuthDto());
        //return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),vo);
    }

    /**
     * 老人信息展示 入院最后一个页面展示信息用
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getOldmanInfoByOldmanId/{oldmanId}")
    public Wrapper getOldmanInfoByOldmanId(@PathVariable Long oldmanId){
        // 校验Id是否为空

        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        BillOldmanInfoVo vo = omcOldmanExpenseItemService.getOldmanInfoByOldmanId(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),vo);
    }

    /**
     * 老人支付项目查询 入院最后一个页面展示信息用
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryPaidItemByOldmanId/{oldmanId}")
    public Wrapper queryPaidItemByOldmanId(@PathVariable Long oldmanId){
        // 校验Id是否为空

        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        OmcOldmanPaidItemVo vo = omcOldmanPaidExpenseItemService.queryPaidItemByOldmanId(oldmanId,getLoginAuthDto());

        //return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),vo);
    }

    /**
     * 查询入院剩余预缴天数
     * @param: [checkInDaysDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryCheckInDays")
    public Wrapper queryCheckInDaysVo(@RequestBody CheckInDaysDto checkInDaysDto){
        //字符串转Date
        checkInDaysDto.setCheckInDate(DateUtil.getDateByString(checkInDaysDto.getCheckInDateString()));
        checkInDaysDto.setGroupId(getLoginAuthDto().getGroupId());
        CheckInDaysVo checkInDaysVo = omcReturnFeeConfigService.queryCheckInDaysVo(checkInDaysDto);
        //return WrapMapper.ok(checkInDaysVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),checkInDaysVo);
    }

    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/oldmanInOfLeyi")
    public Wrapper oldmanInOfLeyi(@RequestBody SelectExpenseDto selectExpenseDto){
        int ret = omcOldmanExpenseItemService.oldmanInOfLeyi(selectExpenseDto,getLoginAuthDto());
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }
}
